package com.meteor.user.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.AllArgsConstructor;
import com.meteor.framework.mybatis.service.impl.BaseServiceImpl;
import com.meteor.user.mapper.SysUserRoleMapper;
import com.meteor.user.entity.SysUserRole;
import com.meteor.user.service.SysUserRoleService;
import com.meteor.user.service.SysUserTokenService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户角色关系
 *
 * @author lynn
 * @date 2024/1/03
 */
@Service
@AllArgsConstructor
public class SysUserRoleServiceImpl extends BaseServiceImpl<SysUserRoleMapper, SysUserRole> implements SysUserRoleService {
    private final SysUserTokenService sysUserTokenService;

    @Override
    public void saveOrUpdate(String userId, List<String> roleIdList) {
        // 数据库角色ID列表
        List<String> dbRoleIdList = getRoleIdList(userId);

        // 需要新增的角色ID
        Collection<String> insertRoleIdList = CollUtil.subtract(roleIdList, dbRoleIdList);
        if (CollUtil.isNotEmpty(insertRoleIdList)) {
            List<SysUserRole> roleList = insertRoleIdList.stream().map(roleId -> {
                SysUserRole entity = new SysUserRole();
                entity.setUserId(userId);
                entity.setRoleId(roleId);
                return entity;
            }).collect(Collectors.toList());

            // 批量新增
            saveBatch(roleList);
        }

        // 需要删除的角色ID
        Collection<String> deleteRoleIdList = CollUtil.subtract(dbRoleIdList, roleIdList);
        if (CollUtil.isNotEmpty(deleteRoleIdList)) {
            LambdaQueryWrapper<SysUserRole> queryWrapper = new LambdaQueryWrapper<>();
            remove(queryWrapper.eq(SysUserRole::getUserId, userId).in(SysUserRole::getRoleId, deleteRoleIdList));
        }
    }

    @Override
    public void saveUserList(String roleId, List<String> userIdList) {
        List<SysUserRole> list = userIdList.stream().map(userId -> {
            SysUserRole entity = new SysUserRole();
            entity.setUserId(userId);
            entity.setRoleId(roleId);
            return entity;
        }).collect(Collectors.toList());

        // 批量新增
        saveBatch(list);

        // 更新用户的缓存权限
        userIdList.forEach(sysUserTokenService::updateCacheAuthByUserId);
    }

    @Override
    public void deleteByRoleIdList(List<String> roleIdList) {
        remove(new LambdaQueryWrapper<SysUserRole>().in(SysUserRole::getRoleId, roleIdList));
    }

    @Override
    public void deleteByUserIdList(List<String> userIdList) {
        remove(new LambdaQueryWrapper<SysUserRole>().in(SysUserRole::getUserId, userIdList));
    }

    @Override
    public void deleteByUserIdList(String roleId, List<String> userIdList) {
        LambdaQueryWrapper<SysUserRole> queryWrapper = new LambdaQueryWrapper<>();
        remove(queryWrapper.eq(SysUserRole::getRoleId, roleId).in(SysUserRole::getUserId, userIdList));

        // 更新用户的缓存权限
        userIdList.forEach(sysUserTokenService::updateCacheAuthByUserId);
    }

    @Override
    public List<String> getRoleIdList(String userId) {
        return baseMapper.getRoleIdList(userId);
    }
}